<template>
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
            <a-form :form="form" slot="detail">
                <a-row>
                    <a-col :span="24">
                        <a-form-item label="物料类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input
                                v-decorator="['huaxuepinleibie',validatorRules.huaxuepinleibie]"
                                placeholder="请输入物料类别"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :span="24">
                        <a-form-item label="货物名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input
                                v-decorator="['huaxuepinmingcheng',validatorRules.huaxuepinmingcheng]"
                                placeholder="请输入货物名称"></a-input>
                        </a-form-item>
                    </a-col>
                  <a-col :span="24">
                    <a-form-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol">
                      <j-dict-select-tag type="radio" v-decorator="['danwei']" :trigger-change="true" dictCode="danwei" placeholder="请选择单位" />
                    </a-form-item>
                  </a-col>
              
             <!--        <a-col :span="24">
                        <a-form-item label="物料特性" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['huaxuepinweixiantexin']"
                                placeholder="请输入物料特性"></a-input>
                        </a-form-item>
                    </a-col> -->
             <!--      <a-col :span="24">
                    <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="图片">
                      <j-image-upload v-decorator="['tupian']" isMultiple></j-image-upload>
                    </a-form-item>
                  </a-col> -->
                    <!-- <a-col :span="24">
            <a-form-item label="二维码" :labelCol="labelCol" :wrapperCol="wrapperCol">
            
              <j-image-upload isMultiple  v-decorator="['erweima']" ></j-image-upload>
            </a-form-item>
          </a-col> -->
                    <!-- <a-col>
            <button @click="getRedis">获取重量</button>
          </a-col> -->

                <!--     <a-col :span="24">
                        <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-input v-decorator="['gongsimingcheng']"
                                placeholder="请输入公司名称"></a-input>
                        </a-form-item>
                    </a-col> -->
                <!--     <a-col :span="24">
                        <a-form-item label="贮存仓库名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
                            <a-select
                                v-decorator="['zhucuncangkumingcheng',validatorRules.zhucuncangkumingcheng]"
                                @change="gethuojia" placeholder="请选择贮存仓库名称">
                                <a-select-option v-for="item in this.cangkulist" :value="item"
                                    :key="item">
                                    {{ item }}
                                </a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col> -->

                    <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
                        <a-button @click="submitForm">提 交</a-button>
                    </a-col>
                </a-row>
            </a-form>
        </j-form-container>
    </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'

export default {
    name: 'YzdWeixianfeiwuxinxiForm',
    components: {
    },
    props: {
        //流程表单data
        formData: {
            type: Object,
            default: () => { },
            required: false
        },
        //表单模式：true流程表单 false普通表单
        formBpm: {
            type: Boolean,
            default: false,
            required: false
        },
        //表单禁用
        disabled: {
            type: Boolean,
            default: false,
            required: false
        }
    },
    data() {
        return {
            huojiahaolist: [],
            cangkulist: [],
            form: this.$form.createForm(this),
            model: {},
            labelCol: {
                xs: { span: 24 },
                sm: { span: 5 },
            },
            wrapperCol: {
                xs: { span: 24 },
                sm: { span: 16 },
            },
            confirmLoading: false,
            
            validatorRules: {
                jingzhong: {
                    rules: [{
                        required: true, message: '请输入净重!'
                    }, {
                        validator: this.validatejingzhong,
                    }]
                },
                pingzhong: {
                    rules: [{
                        required: true, message: '请输入瓶重!'
                    }, {
                        validator: this.validatepingzhong,
                    }]
                },
                huaxuepinmingcheng: {
                    rules: [{
                        required: true, message: '请输入货物名称!'
                    }, {
                        validator: this.validatehuaxuepinmingcheng,
                    }]
                },
                huaxuepinleibie: {
                    rules: [{
                        required: true, message: '请输入货物类别!'
                    }, {
                        validator: this.validatehuaxuepinleibie,
                    }]
                },
                cashao: {
                    rules: [{
                        required: true, message: '请输入cas号!'
                    }, {
                        validator: this.validatecashao,
                    }]
                },
                shengchanchangjia: {
                    rules: [{
                        required: true, message: '请输入生产厂家!'
                    }, {
                        validator: this.validateshengchanchangjia,
                    }]
                },
                zhucuncangkumingcheng: {
                    rules: [{
                        required: true, message: '请选择贮存仓库!'
                    }, {
                        validator: this.validatezhucuncangkumingcheng,
                    }]
                },
                zhucunhuojiamingcheng: {
                    rules: [{
                        required: true, message: '请选择贮存货架!'
                    }, {
                        validator: this.validatezhucunhuojiamingcheng,
                    }]
                },
            },

            url: {
                add: "/yzd_jichuxinxi/yzdWeixianfeiwuxinxi/add",
                edit: "/yzd_jichuxinxi/yzdWeixianfeiwuxinxi/edit",
                queryById: "/yzd_jichuxinxi/yzdWeixianfeiwuxinxi/queryById",
                getCangku: "/yzd_jichuxinxi/yzdZhucuncangkuxinxi/huoQuCangKu",
                gethuojia: "/yzd_jichuxinxi/yzdZhucuncangkuxinxi/findhuojiahao",
                getRedis: '/yzd_rukuguanli/yzdShenqingguanli/getRedis',
            }
        }
    },
    computed: {
        formDisabled() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return false
                }
                return true
            }
            return this.disabled
        },
        showFlowSubmitButton() {
            if (this.formBpm === true) {
                if (this.formData.disabled === false) {
                    return true
                }
            }
            return false
        }
    },
    created() {
        //如果是流程中表单，则需要加载流程表单data
        this.showFlowData();
        this.getCangkumingcheng();
    },
    methods: {
        //获取重量赋值净重
        onSearch1() {
            getAction(this.url.getRedis).then(res => {
                console.log(res.result);
                this.form.setFieldsValue({
                    // jingzhong: res.result
                })
                // if(res.result != '等待传输'){
                //   this.form.setFieldsValue({
                //     jingzhong: res.result
                //   })
                // }
            })
        },
        //获取重量赋值瓶重
        onSearch() {
            getAction(this.url.getRedis).then(res => {
                console.log(res.result);
                this.form.setFieldsValue({
                    pingzhong: 111
                })
                // if(res.result != '等待传输'){
                //   this.form.setFieldsValue({
                //     pingzhong: res.result
                //   })
                // }
            })
        },
        // 获取货架号
        gethuojia(arr) {
            console.log(arr);
            var re = {
                "cangkumingcheng": arr
            }
            getAction(this.url.gethuojia, re).then((res) => {
                console.log(res);
                res.result.forEach(element => {
                    this.huojiahaolist.push(element.huojiahao)
                })
            })
        },

        // 获取仓库名
        getCangkumingcheng() {
            getAction(this.url.getCangku).then(res => {
                console.log(res);
                res.result.forEach(element => {
                    this.cangkulist.push(element.cangkumingcehng)
                    console.log(this.cangkulist)
                });
            })
        },

        add() {
            this.edit({});
        },
        edit(record) {
            this.form.resetFields();
            this.model = Object.assign({}, record);
            this.visible = true;
            this.$nextTick(() => {
                this.form.setFieldsValue(pick(this.model,'danwei','huaxuepinleibie', 'huaxuepinmingcheng', 'cashao', 'huaxuepinweixiantexin', 'msds', 'erweima', 'pingzhong', 'jingzhong', 'zongzhong', 'tupian', 'shengchanchangjia', 'gongsimingcheng', 'zhucuncangkumingcheng', 'zhucunhuojiamingcheng'))
            })
        },
        //渲染流程表单数据
        showFlowData() {
            if (this.formBpm === true) {
                let params = { id: this.formData.dataId };
                getAction(this.url.queryById, params).then((res) => {
                    if (res.success) {
                        this.edit(res.result);
                    }
                });
            }
        },
        submitForm() {
            const that = this;
            // 触发表单验证
            this.form.validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true;
                    let httpurl = '';
                    let method = '';
                    if (!this.model.id) {
                        httpurl += this.url.add;
                        method = 'post';
                    } else {
                        httpurl += this.url.edit;
                        method = 'put';
                    }
                    let formData = Object.assign(this.model, values);
                    console.log("表单提交数据", formData)
                    httpAction(httpurl, formData, method).then((res) => {
                        if (res.success) {
                            that.$message.success(res.message);
                            that.$emit('ok');
                        } else {
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                    })
                }

            })
        },
        popupCallback(row) {
            this.form.setFieldsValue(pick(row, 'huaxuepinleibie', 'huaxuepinmingcheng', 'cashao', 'huaxuepinweixiantexin', 'msds', 'erweima', 'pingzhong', 'jingzhong', 'zongzhong', 'tupian', 'shengchanchangjia', 'gongsimingcheng', 'zhucuncangkumingcheng', 'zhucunhuojiamingcheng','danwei'))
        },
    }
}
</script>